import 'package:flutter/material.dart';

/// 纵向虚线组件
/// 高度由外部指定
class DnpDottedLineWidget extends StatelessWidget {
  /// 虚线高度（由外部指定）
  final double height;

  /// 虚线颜色
  final Color color;

  /// 虚线段的长度
  final double dashLength;

  /// 虚线段之间的间隙长度
  final double dashGap;

  /// 虚线的粗细
  final double strokeWidth;

  const DnpDottedLineWidget({
    Key? key,
    required this.height,
    this.color = Colors.grey,
    this.dashLength = 5.0,
    this.dashGap = 3.0,
    this.strokeWidth = 1.0,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      size: Size(strokeWidth, height),
      painter: _DottedLinePainter(
        color: color,
        dashLength: dashLength,
        dashGap: dashGap,
        strokeWidth: strokeWidth,
      ),
    );
  }
}

/// 虚线画笔
class _DottedLinePainter extends CustomPainter {
  final Color color;
  final double dashLength;
  final double dashGap;
  final double strokeWidth;

  _DottedLinePainter({
    required this.color,
    required this.dashLength,
    required this.dashGap,
    required this.strokeWidth,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..strokeWidth = strokeWidth
      ..style = PaintingStyle.stroke;

    double startY = 0;
    final x = size.width / 2;

    // 绘制纵向虚线
    while (startY < size.height) {
      final endY = (startY + dashLength) > size.height
          ? size.height
          : startY + dashLength;
      canvas.drawLine(Offset(x, startY), Offset(x, endY), paint);
      startY += dashLength + dashGap;
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return oldDelegate != this;
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is _DottedLinePainter &&
        other.color == color &&
        other.dashLength == dashLength &&
        other.dashGap == dashGap &&
        other.strokeWidth == strokeWidth;
  }

  @override
  int get hashCode {
    return Object.hash(
      color,
      dashLength,
      dashGap,
      strokeWidth,
    );
  }
}

/// 横向虚线组件
/// 宽度和高度由外部指定
class DnpHorizontalDottedLineWidget extends StatelessWidget {
  /// 虚线宽度（由外部指定）
  final double width;

  /// 虚线高度（控制组件占据的垂直空间）
  final double height;

  /// 虚线颜色
  final Color color;

  /// 虚线段的长度
  final double dashLength;

  /// 虚线段之间的间隙长度
  final double dashGap;

  /// 虚线的粗细
  final double strokeWidth;

  /// 左边距
  final double marginLeft;

  /// 右边距
  final double marginRight;

  const DnpHorizontalDottedLineWidget({
    Key? key,
    required this.width,
    this.height = 1.0,
    this.color = Colors.grey,
    this.dashLength = 5.0,
    this.dashGap = 3.0,
    this.strokeWidth = 1.0,
    this.marginLeft = 0.0,
    this.marginRight = 0.0,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(left: marginLeft, right: marginRight),
      child: CustomPaint(
        size: Size(width, height),
        painter: _HorizontalDottedLinePainter(
          color: color,
          dashLength: dashLength,
          dashGap: dashGap,
          strokeWidth: strokeWidth,
        ),
      ),
    );
  }
}

/// 横向虚线画笔
class _HorizontalDottedLinePainter extends CustomPainter {
  final Color color;
  final double dashLength;
  final double dashGap;
  final double strokeWidth;

  _HorizontalDottedLinePainter({
    required this.color,
    required this.dashLength,
    required this.dashGap,
    required this.strokeWidth,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..strokeWidth = strokeWidth
      ..style = PaintingStyle.stroke;

    double startX = 0;
    final y = size.height / 2;

    // 绘制横向虚线
    while (startX < size.width) {
      final endX = (startX + dashLength) > size.width
          ? size.width
          : startX + dashLength;
      canvas.drawLine(Offset(startX, y), Offset(endX, y), paint);
      startX += dashLength + dashGap;
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return oldDelegate != this;
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is _HorizontalDottedLinePainter &&
        other.color == color &&
        other.dashLength == dashLength &&
        other.dashGap == dashGap &&
        other.strokeWidth == strokeWidth;
  }

  @override
  int get hashCode {
    return Object.hash(
      color,
      dashLength,
      dashGap,
      strokeWidth,
    );
  }
}

